package org.remixlab.gnutopic.data;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.remixlab.gnutopic.pojo.ForumCategory;
import org.remixlab.gnutopic.pojo.ForumThread;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class XmlParser {

	private String path;
	
	private ArrayList<ForumCategory> categories;
	
	private TreeMap<Date, ForumThread> threads;
	
	private static XmlParser instance;
	
	private XmlParser(){
		path = "data/forum_processing.xml";
		
		//inicializo el arreglo..
		categories = new ArrayList<ForumCategory>();
		
		//aqui inicializo el treeMap de hilos...
		threads = new TreeMap<Date, ForumThread>();
		
	}
	
	public static XmlParser getInstance(){
		if(instance==null){
			instance = new XmlParser();
		}
		
		return instance;
	}
	
	/**
	 * Lee el archivo xml y crea los objetos en memoria.
	 */
	public void parseXmlFile(){
		
		try {
			File file = new File(path);
			InputStream inputStream= new FileInputStream(file);
			Reader reader = new InputStreamReader(inputStream,"UTF8");
			 
			InputSource is = new InputSource(reader);
			is.setEncoding("UTF8");
			
			
			SAXParserFactory factory = SAXParserFactory.newInstance();
			SAXParser saxParser = factory.newSAXParser();
			
			XmlHandler xmlHandler = new XmlHandler(); 
			
			saxParser.parse(is, xmlHandler);
			
			categories = xmlHandler.getCategories();
			
			threads = xmlHandler.getThreads();
			
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * Se encarga de consultar una palabra de texto en el apache solr
	 * @param query
	 */
	public Map<String, ArrayList<Integer>> search(String query){
		
		Map<String, ArrayList<Integer>> result = new HashMap<String, ArrayList<Integer>>();  
		
		try {
			String q = URLEncoder.encode(query, "UTF-8");
			
			URL url = new URL ("http://localhost:8983/solr/select/?q="+q+"&start=0&rows=15000&indent=on");
			
			URLConnection urlConnection = url.openConnection();
			
			InputStream inputStream = urlConnection.getInputStream();
			
			BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
			
			InputSource is = new InputSource(in);
			is.setEncoding("UTF8");
			
			
			SAXParserFactory factory = SAXParserFactory.newInstance();
			SAXParser saxParser = factory.newSAXParser();
			
			XmlSolrHandler xmlHandler = new XmlSolrHandler(); 
			
			saxParser.parse(is, xmlHandler);
			
			result = xmlHandler.getThreads();
			
			in.close();
			
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return result;
	}
	
	public String getPath() {
		return path;
	}


	public void setPath(String path) {
		this.path = path;
	}


	public ArrayList<ForumCategory> getCategories() {
		return categories;
	}


	public void setCategories(ArrayList<ForumCategory> categories) {
		this.categories = categories;
	}


	public TreeMap<Date, ForumThread> getThreads() {
		return threads;
	}


	public void setThreads(TreeMap<Date, ForumThread> threads) {
		this.threads = threads;
	}


	public static void main (String [] args){
		XmlParser parser = new XmlParser();
		
		//proceso el archivo
		/*parser.parseXmlFile();
		
		ArrayList<ForumCategory> categories = parser.getCategories();
		
		for(ForumCategory category : categories){
			System.out.println(category);
		}*/
		
		parser.search("processing");
		
	}
	
}
